﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Work.UI.Web.Forms.Code.Common;
using Work.UI.Web.Forms.Code.Security;
using System.Threading;

namespace Work.UI.Web.Forms.Admin
{
    public partial class Admin : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Header.DataBind();

            if (this.SecurityService.IsAuthotificate())
            {
                loginA.Visible = true;
                loginControl.Visible = false;
                lblMenu.Visible = true;
            }
            else
            {
                loginA.Visible = false;
                loginControl.Visible = true;
                lblMenu.Visible = false;
            }
            
        }

        protected ISercurityService SecurityService
        {
            get
            {
                return IoCContainer.ServiceLocator.Resolve<ISercurityService>();
            }
        }

        protected void loginControl_LoggedIn(object sender, EventArgs e)
        {
            string redirectUrl = Request["ReturnUrl"];
            if (!string.IsNullOrWhiteSpace(redirectUrl))
            {
                Response.Redirect(redirectUrl);
            }
            else
            {
                Response.Redirect("~/Admin/Default.aspx");
            }
        }

        protected void loginControl_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string userName = loginControl.UserName.ToLower();
            string password = loginControl.Password;
            bool remember = loginControl.RememberMeSet;
            e.Authenticated = this.SecurityService.Login(userName, password, remember);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            
        }
    }
}